package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqTenantAccount;
import cloud.maque.admin.mapper.MqTenantAccountMapper;
import cloud.maque.admin.service.MqTenantAccountService;
import cloud.maque.framework.common.exception.BusinessException;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

/**
 * @author zhuyf
 * @date 2023/4/25 19:29
 */
@Service
public class MqTenantAccountServiceImpl extends ServiceImpl<MqTenantAccountMapper, MqTenantAccount>
        implements MqTenantAccountService {

    @Override
    public MqTenantAccount saveAccount(MqTenantAccount account) {

        Assert.hasText(account.getAppKey(), "AppKey不能为空");
        Assert.hasText(account.getAccountType(), "账户类型不能为空");

        //判断同类型的是否存在
        List<MqTenantAccount> finds = list(account);
        Optional<MqTenantAccount> oop = finds.stream().filter(item -> !item.getId().equals(account.getId())).findFirst();
        MqTenantAccount exits = null;
        if (!oop.equals(Optional.empty())) {
            exits = oop.get();
        }
        if (finds.size() > 0 && exits != null) {
            throw new BusinessException("账户类型已经存在，不能再创建");
        }

        this.saveOrUpdate(account);
        return account;
    }

    @Override
    public List<MqTenantAccount> list(PaginateDataResquest<MqTenantAccount> account) {
        QueryWrapper<MqTenantAccount> wrapper = new QueryWrapper<>();

        if (account != null && account.getData() != null) {
            if (!StrUtil.isEmptyIfStr(account.getData().getTenantId())) {
                wrapper.lambda().eq(MqTenantAccount::getTenantId, account.getData().getTenantId());
            }

            if (!StrUtil.isEmptyIfStr(account.getData().getAccountType())) {
                wrapper.lambda().eq(MqTenantAccount::getAccountType, account.getData().getAccountType());
            }

            if (!StrUtil.isEmptyIfStr(account.getData().getDisabled())) {
                wrapper.lambda().eq(MqTenantAccount::getDisabled, account.getData().getDisabled());
            }

        }
        wrapper.lambda().orderByAsc(MqTenantAccount::getCreateAt);
        List<MqTenantAccount> lists = this.baseMapper.selectList(wrapper);

        return lists;
    }

    @Override
    public List<MqTenantAccount> list(MqTenantAccount account) {
        QueryWrapper<MqTenantAccount> wrapper = new QueryWrapper<>();

        if (account != null) {
            if (!StrUtil.isEmptyIfStr(account.getTenantId())) {
                wrapper.lambda().eq(MqTenantAccount::getTenantId, account.getTenantId());
            }

            if (!StrUtil.isEmptyIfStr(account.getAccountType())) {
                wrapper.lambda().eq(MqTenantAccount::getAccountType, account.getAccountType());
            }

            if (!StrUtil.isEmptyIfStr(account.getDisabled())) {
                wrapper.lambda().eq(MqTenantAccount::getDisabled, account.getDisabled());
            }

        }
        wrapper.lambda().orderByAsc(MqTenantAccount::getCreateAt);
        List<MqTenantAccount> lists = this.baseMapper.selectList(wrapper);

        return lists;
    }

    @Override
    public MqTenantAccount getAccountByType(MqTenantAccount account) {
        QueryWrapper<MqTenantAccount> wrapper = new QueryWrapper<>();

        if (account != null) {
            if (!StrUtil.isEmptyIfStr(account.getTenantId())) {
                wrapper.lambda().eq(MqTenantAccount::getTenantId, account.getTenantId());
            }
            if (!StrUtil.isEmptyIfStr(account.getAccountType())) {
                wrapper.lambda().eq(MqTenantAccount::getAccountType, account.getAccountType());
            }
            if (!StrUtil.isEmptyIfStr(account.getDisabled())) {
                wrapper.lambda().eq(MqTenantAccount::getDisabled, account.getDisabled());
            }

        }
        wrapper.lambda().orderByAsc(MqTenantAccount::getCreateAt);
        return this.getOne(wrapper, false);

    }

    @Override
    public Boolean delete(String id, String tenantId) {

        QueryWrapper<MqTenantAccount> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqTenantAccount::getTenantId, tenantId);
        wrapper.lambda().eq(MqTenantAccount::getId, id);
        return remove(wrapper);
    }
}
